package com.hn.shopadmin.controller;

import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopcommon.result.CommonPage;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import com.hn.kiss.modules.sys.shopdb.entity.Jurisdiction;
import com.hn.shopadmin.service.JurisdictionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Api(tags = "JruisdictionController", description = "权限管理")
@RestController
@RequestMapping("/jrui")
@Validated
public class JurisdictionController {
    @Autowired
    private JurisdictionService jurisdictionService;

    @ApiOperation("一级权限添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "权限路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "describes", value = "权限描述", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/insertParent")
    public CommonResult insertParent(@NotBlank(message = "url不能为空") String url,
                                     @NotBlank(message = "描述不能为空") String describes)throws ParmException {
        int i = jurisdictionService.insertParent(url, describes);
        if (i > 0){
            return CommonResult.success("权限添加成功");
        }
        return CommonResult.failed("权限添加失败");
    }

    @ApiOperation("一级权限添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "url", value = "权限路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父权限id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "describes", value = "权限描述", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/insertChild")
    public CommonResult insertChild(@NotBlank(message = "url不能为空") String url,
                                    @NotBlank(message = "父权限id不能为空") String parentId,
                                    @NotBlank(message = "描述不能为空") String describes)throws ParmException {
        int i = jurisdictionService.insertChild(url, parentId, describes);
        if (i > 0){
            return CommonResult.success("权限添加成功");
        }
        return CommonResult.failed("权限添加失败");
    }

    @ApiOperation("子权限删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/delteChild")
    public CommonResult deleteChild(@NotBlank(message = "id不能为空") String id){
        int i = jurisdictionService.deleteChild(id);
        if (i > 0){
            return CommonResult.success("权限删除成功");
        }
        return CommonResult.failed("权限删除失败");
    }

    @ApiOperation("一级权限删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/deleteParent")
    public CommonResult deleteParent(@NotBlank(message = "id不能为空") String id){
        int i = jurisdictionService.deletePrent(id);
        if (i > 0){
            return CommonResult.success("权限删除成功");
        }
        return CommonResult.failed("权限删除失败");
    }


    @ApiOperation("权限信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "url", value = "权限路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "describes", value = "权限描述", dataType = "String", paramType = "query")
    })
    @PostMapping("/update")
    public CommonResult update(@NotBlank(message = "id不能为空") String id,
                               @NotBlank(message = "url不能为空") String url,
                               String describes)throws ParmException{
        int update = jurisdictionService.update(id, url, describes);
        if (update > 0){
            return CommonResult.success("权限信息修改成功");
        }
        return CommonResult.failed("权限信息修改失败");
    }

    @ApiOperation("通过id或取权限信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
    @GetMapping("/getById")
    public CommonResult<Jurisdiction> getById(@NotBlank(message = "id不能为空") String id){
        Jurisdiction byId = jurisdictionService.getById(id);
        return CommonResult.success(byId);
    }



    @ApiOperation("一级权限分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "url/描述", dataType = "String", paramType = "query")
    })
    @GetMapping("/parentListPage")
    public CommonPage<Jurisdiction> parentListPage(@NotNull(message = "page不能为空") Integer page,
                                                   @NotNull(message = "limit不能为空") Integer limit,
                                                   String keywork){
        PageInfo<Jurisdiction> jurisdictionPageInfo = jurisdictionService.listPage(page, limit, "0", keywork);
        return CommonPage.restPage(jurisdictionPageInfo);
    }

    @ApiOperation("子权限分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父资源id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "url/描述", dataType = "String", paramType = "query")
    })
    @GetMapping("/childListPage")
    public CommonPage<Jurisdiction> childListPage(@NotNull(message = "page不能为空") Integer page,
                                                  @NotNull(message = "limit不能为空") Integer limit,
                                                  String parentId,
                                                  String keywork){
        PageInfo<Jurisdiction> jurisdictionPageInfo = jurisdictionService.listPage(page, limit, parentId, keywork);
        return CommonPage.restPage(jurisdictionPageInfo);
    }

}
